package mvc;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
public class WebController {
	ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
	NoticeJDBCTemplate jdbc = (NoticeJDBCTemplate) context.getBean("noticeJDBCTemplate");
	@RequestMapping(value="/")
	public String index() {
		return "index";
	}
	@RequestMapping(value="/list")
	public String list(ModelMap m) {
		List<Notice> list = jdbc.listNotices();
		m.addAttribute("list", list);
		return "list";
	}
	@RequestMapping(value="/delete",method = RequestMethod.POST)
	@ResponseBody
	public String delete(@RequestParam(value = "id[]") Integer[] id) {
		for (Integer i : id) {
			Integer result = jdbc.delete(i);
			if(result.equals(0)) return "fail";
		}
	    return "success";
	}
}
